package com.example.lib_log

import com.example.lib_log.logger.EmailLogger
import com.example.lib_log.logger.FileLogger
import com.example.lib_log.logger.LogCatLogger
import com.example.lib_log.logger.NetLogger

/**
 * @author yaotianxue
 * @description
 * @date 2024/10/21
 */
class Logger private constructor(private val TAG:String, private val level:LoggerLevel, private val loggerStrategy: LoggerStrategy, private val isDebug:Boolean, private val saveUrl:String){
    private var logger:ILogger
    init {
        logger = getLogger()
    }
    //次构造继承主构造
    constructor(builder: Builder):this(
        builder.TAG,
        builder.level,
        builder.loggerStrategy,
        builder.isDebug,
        builder.saveUrl
    )

    /**
     * 策略者模式：根据策略创建对应logger
     */
    fun getLogger():ILogger{
        when(loggerStrategy){
            LoggerStrategy.EMAIL -> logger = EmailLogger()
            LoggerStrategy.FILE -> logger = FileLogger()
            LoggerStrategy.NET -> logger = NetLogger()
            LoggerStrategy.LOGCAT -> logger = LogCatLogger()
        }
        logger.setLoggerLevel(level)
        logger.setLogTAG(TAG)
        logger.setDebug(isDebug)
        logger.setSaveUrl(saveUrl)
        return logger
    }

    /**
     * 输出Debug log
     */
    fun d(Tag:String,log:String){
        logger.d(Tag,log)
    }

    /**
     * 输出Warn log
     */
    fun w(Tag:String,log:String){
        logger.w(Tag,log)
    }

    /**
     * 输出Verbose log
     */
    fun v(Tag:String,log:String){
        logger.v(Tag,log)
    }

    /**
     * 输出Error log
     */
    fun e(Tag:String,log:String){
        logger.e(Tag,log)
    }
    /**
     * 输出Info log
     */
    fun i(Tag:String,log:String){
        logger.i(Tag,log)
    }



    /**
     * 构建者模式：属性比较多情况，按需设置
     */
    class Builder{
        var TAG:String = "默认TAG"
        var level:LoggerLevel = LoggerLevel.Debug
        var loggerStrategy: LoggerStrategy = LoggerStrategy.LOGCAT
        var isDebug:Boolean = true
        var saveUrl:String = "默认路径"
        fun setTag(TAG:String):Builder {
            this.TAG = TAG
            return this
        }
        fun setLevel(level:LoggerLevel):Builder{
            this.level = level
            return this
        }

        fun setLoggerStrategy(loggerStrategy: LoggerStrategy):Builder{//设置策略
            this.loggerStrategy = loggerStrategy
            return this
        }

        fun isDebug(isDebug: Boolean):Builder{
            this.isDebug = isDebug
            return this
        }


        fun setSaveUrl(saveUrl: String):Builder{
            this.saveUrl = saveUrl
            return this
        }

        fun build():Logger{
            return Logger(this)
        }

    }
}